home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / manual-p / man_db-2.000 / man_db-2 / man_db-2.3.10 / xcat / xcat.l < prev   
Encoding:
Lex Description  |  1995-05-08  |  4.4 KB  |  190 lines

  1. %{
  2.  
  3. /*
  4.  * xcat: produce .m file from .c file for gencat 
  5.  *  
  6.  * Copyright (C), 1994, 1995, Graeme W. Wilford. (Wilf.)
  7.  *
  8.  * You may distribute under the terms of the GNU General Public
  9.  * License as specified in the file COPYING that comes with this
  10.  * distribution.
  11.  *
  12.  * This program should be used with C programs containing certain 
  13.  * directives, specifically CATGETS() and NLS_SET should be #defined.
  14.  *
  15.  * usage: xcat Register < foo.c > foo.m
  16.  *
  17.  * Wed Oct 12 18:46:11 BST 1994  Wilf. (G.Wilford@ee.surrey.ac.uk) 
  18.  */
  19.  
  20. /* This program provides a basic method of producing message catalogues
  21.    for C source files. Each C source file must define NLS_SET prior to
  22.    #including "nls.h", which will set up the CATGETS() macro. eg
  23.  
  24.    #define NLS_SET    <file_id>Set
  25.    #include "nls.h"
  26.  
  27.    where <file_id> is a unique file id such as the filename itself.
  28.    From then on, you may use the following CATGETS() function instead of 
  29.    the library function catgets(). The CATGETS function is defined:
  30.  
  31.    char * CATGETS(int id, char *message)
  32.  
  33.    where id is the name <file_id>_<message_id>, message_id being any upper
  34.    case name that uniquely identifies the message. Identical messages may
  35.    have identical message id's. 
  36.  
  37.    gencat can be used to process the message file produced, and should also
  38.    be used to create a macro header file. This header will contain definitions
  39.    of all of the <file_id>_<message_id> names you used. These map to integers.
  40.  */
  41.  
  42. #define MAX_MSGS    100    /* maximum number of messages per C-file */
  43. #define NLS_TEXT    ""    /* bogus string prepended to each message
  44.                    for debugging purposes, set to "" if
  45.                    preferred  */ 
  46. #define STATIC_VER
  47.  
  48. #ifdef HAVE_CONFIG_H
  49. #  include "config.h"
  50. #endif /* HAVE_CONFIG_H */
  51.  
  52. #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
  53. #  include <string.h>
  54. #elif defined(HAVE_STRINGS_H)
  55. #  include <strings.h>
  56. #endif /* STDC_HEADERS */
  57.  
  58. #ifdef HAVE_UNISTD_H
  59. #  include <unistd.h>
  60. #endif /* HAVE_UNISTD_H */
  61.  
  62. #include <ctype.h>
  63.  
  64. #include "manconfig.h"
  65. #include "lib/error.h"
  66.  
  67. static int get_set_number(void);
  68.  
  69. char *program_name = "xcat";
  70. static short Set_len;
  71. static char ch, *register_file = NULL;
  72. static char buffer[4096];
  73. %}
  74.  
  75. %x in_string
  76. %x catgets_arg1
  77. %x catgets_rest
  78. %x in_Set
  79. %x normal
  80.  
  81. %%
  82.  
  83. #define[ \t]+NLS_SET[ \t]+     BEGIN(in_Set);
  84. <in_Set>[^ \t\n"]+       {
  85.             yytext[yyleng - 3] = '\0';
  86.             printf("$set %d #%s\n", get_set_number(), yytext);
  87.             Set_len = yyleng - 3;
  88.             BEGIN(normal);
  89.            }
  90.  
  91. <normal>CATGETS[ \t\n]*\([ \t\n]*    BEGIN(catgets_arg1);
  92. <catgets_arg1>[^" \n\t,]+  {
  93.             static struct {
  94.                 char *text;
  95.                 int seen;
  96.             } message[MAX_MSGS];
  97.             static int msg_no = 0;
  98.             int i = 0;
  99.  
  100.             /* don't duplicate messages with identical id's */
  101.             while (i != msg_no) {
  102.                 if (strcmp(message[i].text, yytext) == 0) {
  103.                     if (!message[i].seen) {
  104.                         error(0, 0, 
  105.                               "warning: duplicated message id: %s",
  106.                               yytext);
  107.                         message[i].seen = 1;
  108.                     }
  109.                     BEGIN(normal);
  110.                     i = -1;
  111.                     break;
  112.                 }
  113.                 i++;
  114.             }
  115.                 
  116.             if (i != -1) {
  117.                 message[msg_no++].text = xstrdup(yytext);
  118.                 yytext += Set_len;
  119.                 printf("\n$ #%s Original Message:", yytext);
  120.                 BEGIN(catgets_rest);
  121.                 ch = '\0';
  122.             }
  123.            }
  124. <catgets_rest>[^"\) \t\n,]*    strcat(buffer, yytext);
  125. <catgets_rest>\)   {
  126.             printf("(%s)\n# " NLS_TEXT "%s\n", buffer, buffer);
  127.             buffer[0] = '\0';
  128.             BEGIN(normal);
  129.            }
  130. <catgets_rest>\"   {    
  131.             if (ch)
  132.                 strcat(buffer, "\\\n");
  133.             BEGIN(in_string);
  134.             ch = '\\';
  135.            }
  136. <catgets_arg1,catgets_rest>[ \n\t,]
  137. <in_string>[^\\"]*    strcat(buffer, yytext);
  138. <in_string>\\\"        putchar(*(yytext + 1));
  139. <in_string>\\[^"]    strcat(buffer, yytext);
  140. <in_string>\"      {
  141.             BEGIN(catgets_rest);
  142.            }
  143. <in_string,catgets_arg1,catgets_rest,in_Set><<EOF>>  {
  144.             error (0, 0, "fatal: EOF within CATGETS() or Set definition");
  145.             yyterminate();
  146.            }
  147. <INITIAL,normal>.|\n
  148.  
  149. %%
  150.  
  151. int main(int argc, char *argv[]) 
  152. {
  153.     if (argc == 2)
  154.         register_file = argv[1];
  155.  
  156.     yylex();
  157.     return 0;
  158. }
  159.  
  160. static int get_set_number(void)
  161. {
  162.     if (register_file) {
  163.         FILE *file;
  164.  
  165.         if ( (file = fopen(register_file, "r")) ) {
  166.             char line[1024];
  167.  
  168.             while ( fgets(line, 1023, file) ) {
  169.                 if (*line == '#')
  170.                     continue;
  171.                 if (strncmp(line, yytext, yyleng - 3) == 0 &&
  172.                     isspace(*(line + yyleng - 3))) {
  173.                     fclose(file);
  174.                     return atoi( line + yyleng - 3 + 1 );
  175.                 }
  176.             }
  177.             error (1, 0, "set \"%s\" in %s not found, aborting",
  178.                    yytext, register_file);
  179.         }
  180.     } 
  181.     return 1;
  182. }
  183.  
  184. /* for compatibility with systems not having libfl */
  185. int yywrap(void)
  186. {
  187.     return 1;
  188. }
  189.  
  190.